"use client";

import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { useAuth } from "@/app/context/auth-context";

interface AdminRouteProps {
  children: React.ReactNode;
}

export default function AdminRoute({ children }: AdminRouteProps) {
  const router = useRouter();
  const { isAuthenticated, isAdmin, checkAuth } = useAuth();
  const [checking, setChecking] = useState(true);

  useEffect(() => {
    const verifyAuth = async () => {
      try {
        const isAdminUser = await checkAuth();
        
        if (!isAdminUser) {
          router.push('/explore/apps');
          return;
        }
        
        setChecking(false);
      } catch (error) {
        console.error('验证权限失败:', error);
        router.push('/signin');
      }
    };

    verifyAuth();
  }, [router, checkAuth]);

  // 显示加载状态
  if (checking) {
    return (
      <div className="flex justify-center items-center h-screen">
        <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500"></div>
      </div>
    );
  }

  // 授权，显示内容
  return <>{children}</>;
} 